#ifndef GRID_H_
#define GRID_H_

#include <iostream>
#include <cmath>
#include <complex>
#define complex std::complex<double>



namespace Grid
{

    void Grid_1d(double* X ,int nx, double dx, double x0);//定步长一维数组
    void Grid_step(double* X ,int nx, double x0, double* step_array); // 变步长一维数组
    
    
    template<typename T>
    void ini_zeros(int row, int column ,T array){
        for(int num_row = 0; num_row < row; num_row ++){
            for(int num_column = 0; num_column < column; num_column ++){
                array[num_row][num_column] = 0.;
            }
        }
    }
    
  
    // template<typename T>
    // void ini_ones (int row, int column ,T array){
    //     for(int num_row = 0; num_row < row; num_row ++){
    //         for(int num_column = 0; num_column < column; num_column ++){
    //             array[num_row][num_column] = 1.;
    //         }
    //     }        
    // }

    
    //!二维空矩阵
    class Grid_2d{
    public:
        //nr * nt
        double** E_n0;     //nr * nt
        complex** E_n1fft;  //nr * fft_num
        //nt * nr
        complex** E_n0fft; //fft_num * nr
        complex** G_n0fft; //fft_num * nr
        complex** G_n_1fft;//fft_num * nr  nz-1,用来存储上一步非线性结果
        
        int m_row, m_column;  //行nr，列nt
        int fft_num;

        explicit Grid_2d(int row, int column) : m_row(row), m_column(column){
            
            fft_num = (m_column % 2 == 0)? int(m_column/2) + 1 : int((m_column-1)/2) + 1;
            

            E_n0 = new double* [m_row];       //nr行
            E_n1fft = new complex* [m_row];   //nr行
            for(int i = 0; i < m_row; ++i){
                E_n0[i] = new double[m_column];     //nt列
                E_n1fft[i] = new complex[fft_num]; //nt列
            }

            E_n0fft = new complex* [fft_num];   //nt行
            G_n0fft = new complex* [fft_num];   //nt行
            G_n_1fft = new complex* [fft_num];   //nt行
            for(int i = 0; i < fft_num; ++i){
                E_n0fft[i] = new complex[m_row]; //nr列
                G_n0fft[i] = new complex[m_row]; //nr列
                G_n_1fft[i] = new complex[m_row]; //nr列
            }

        }

        virtual ~Grid_2d(){
            std::cout << "   " <<"grid_2d析构"<<std::endl;
            for (int i = 0; i < fft_num; ++i) {
                delete[] E_n0fft[i];
                delete[] G_n0fft[i];
                delete[] G_n_1fft[i];
            }
            delete[] E_n0fft;
            delete[] G_n0fft;
            delete[] G_n_1fft;

            for (int i = 0; i < m_row; ++i) {
                delete[] E_n1fft[i];
                delete[] E_n0[i];
            }
            delete[] E_n1fft;
            delete[] E_n0;
        }
    };


} // namespace Grid


//! 一维网格划分
void Grid::Grid_1d(double* X ,int nx, double dx, double x0){ //输入数组长度nx，数组间隔dx，数组第一个数据x0，得到定步长数组X的元素
    
    for(int ix = 0; ix < nx; ix++){
        X[ix] = x0 + ix*dx;
        // cout<<X[ix]<<endl;
    }
}
void Grid::Grid_step(double* X ,int nx, double x0, double* step_array){ //输入数组长度nx，数组间隔dx，数组第一个数据x0，步长数组step_array, 得到变步长数组X的元素
    
    double xn = x0;
    for(int ix = 0; ix < nx; ix++){
        X[ix] = xn;
        xn += step_array[ix];
    }
    
}


#endif